home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-13
/
trs141f.zip
/
NFMT.ZIP
/
OPTIONS.H
< prev
next >
Wrap
Text File
|
1992-07-20
|
3KB
|
137 lines
/*
* Option processing.
*
* Conforms to intro(1) in System V.
*
* Typical layout:
*
* OPTIONS ("-b -t c -f file -w N +b file ...")
* FLAG ('b', flag)
* CHAR ('t', tab_ch)
* STRING ('f', filename)
* f = fopen(filename, "r");
* NUMBER ('w', width)
* MORE_OPTIONS('+')
* FLAG ('b', unflag)
* ENDOPTS
*
* AUTHOR
* Ross Paterson <rap@doc.ic.ac.uk>
* All rights reserved.
*/
char *O_name, *O_usage;
void usage ARGS((void));
void
usage()
{
/*
* Poor man's version of:
*
* fprintf(stderr, "Usage: %s %s\n", O_name, O_usage);
*/
write(2, "Usage: ", 7);
write(2, O_name, strlen(O_name));
write(2, " ", 1);
write(2, O_usage, strlen(O_usage));
write(2, "\n", 1);
exit(1);
}
/*
* An argument "-" is interpreted as a program argument and stops
* option processing.
*
* An argument "--" stops option processing and is discarded.
*
* An option which takes an argument uses either the rest of the
* the current argument, or, if at the end of an argument, the
* next argument.
*/
#define OPTIONS(usage) \
O_usage = usage; \
O_name = argv[0]; \
while (*++argv) { \
register int O_cont = 1; \
\
if (**argv == '-') { \
if (argv[0][1] == '\0') \
break; \
argc--; \
if (argv[0][1] == '-' && argv[0][2] == '\0') { \
argv++; \
break; \
} \
while (O_cont) \
switch (*++*argv) { \
case '\0': \
O_cont = 0;
#define FLAG(c,flag) break; \
case c: \
flag = 1;
#define NUMBER(c,num) break; \
case c: \
if (*++*argv == '\0') { \
if (--argc == 0) \
usage(); \
argv++; \
} \
num = atoi(*argv); \
O_cont = 0;
#define NUM_OPT(num) break; \
case '0': case '1': case '2': case '3': \
case '4': case '5': case '6': case '7': \
case '8': case '9': \
num = atoi(*argv); \
O_cont = 0;
#define STRING(c,str) break; \
case c: \
if (*++*argv == '\0') { \
if (--argc == 0) \
usage(); \
argv++; \
} \
str = *argv; \
O_cont = 0;
#define CHAR(c,ch) break; \
case c: \
if (*++*argv == '\0') { \
if (--argc == 0) \
usage(); \
argv++; \
} \
ch = **argv; \
O_cont = 0;
#define MORE_OPTIONS(c) break; \
default: \
usage(); \
} \
} \
else if (**argv == (c)) { \
if (argv[0][1] == '\0') \
break; \
argc--; \
while (O_cont) \
switch (*++*argv) { \
case '\0': \
O_cont = 0;
#define ENDOPTS break; \
default: \
usage(); \
} \
} \
else \
break; \
} \
*--argv = O_name;